//
//  M1CertifyEducationVC.m
//  NewMarryU
//
//  Created by linl on 2018/7/19.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1CertifyEducationVC.h"

@interface M1CertifyEducationVC ()

@property (nonatomic, strong) M1RowPickView *m1_pickEdu;
@property (nonatomic, strong) M1RowPickView *m1_pickSchool;

@property (nonatomic, strong) NSMutableDictionary *m1_paramDic;

@end

@implementation M1CertifyEducationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.m1_photos addObject:@"cer_edu_exp"];
    
    self.m1_desDic = @{@"img":@"cer_edu_exp",@"des1":@"学历证明范例",@"des2":@"添加学历证明"};
    
    if (self.m1_paramDic) {};
    
    [self.m1_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
    [self.m1_collectionView reloadData];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    CGFloat bottom = 50;
    
    CGFloat top = 150;
    
    CGFloat width = (SCREENWIDTH - 16.0 - 30.0 - 30.0) / 2;
    
    CGFloat scale = 112.0 / 151.0;
    
    CGFloat height = width * scale + 30;
    
    NSInteger count = [self.m1_photos count] > self.m1_maxPhoto ? [self.m1_photos count] : [self.m1_photos count] + 1;
    
    NSInteger rate = count/2 + (count%2 == 0 ? 0 : 1);
    
    CGFloat c_height = rate * height + (rate - 1)*16;
    
    
    CGRect frame = self.m1_collectionView.frame;
    frame.size.height = top + c_height + bottom;
    self.m1_collectionView.frame = frame;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark lazy load

- (M1RowPickView *)m1_pickEdu
{
    if (!_m1_pickEdu) {
        _m1_pickEdu = [[M1RowPickView alloc] init];
        _m1_pickEdu.m1_paramDic = [@{
                                      m1_RowPickerKeyHeight:@(54),
                                      m1_RowPickerKeyName:@"最高学历",
                                      m1_RowPickerKeyValue:[self m1_EduName:self.m1_paramDic[@"education_code"]],
                                      m1_RowPickerKeyType:@(EnumPickType_button),
                                      m1_RowPickerKeyHasArrow:@(1),
                                      m1_RowPickerKeyHoldText:@"请选择你的最高学历",
                                      m1_RowPickerKeyKeyHasLine:@(1)
                                      } mutableCopy];
        _m1_pickEdu.m1_edgeInsetsRow = UIEdgeInsetsMake(0, 20, 0, 20);
        [_m1_pickEdu m1_updateData];
        WEAKSELF
        [_m1_pickEdu setM1_resultBlock:^(M1RowPickView *row) {
            [BRStringPickerView showStringPickerWithTitle:@"" dataSource:@[@"专科",@"本科",@"硕士",@"博士",@"博士后"] defaultSelValue:nil resultBlock:^(id selectValue) {
                [row m1_updateValue:selectValue value:selectValue];
                [weakSelf m1_enableRightBtn];
                [weakSelf.m1_paramDic setObject:[weakSelf m1_EduCode:selectValue] forKey:@"education_code"];
            }];
        }];
    }
    return _m1_pickEdu;
}

- (M1RowPickView *)m1_pickSchool
{
    if (!_m1_pickSchool) {
        _m1_pickSchool = [[M1RowPickView alloc] init];
        _m1_pickSchool.m1_paramDic = [@{
                                      m1_RowPickerKeyHeight:@(54),
                                      m1_RowPickerKeyName:@"毕业院校",
                                      m1_RowPickerKeyValue:self.m1_paramDic[@"school"],
                                      m1_RowPickerKeyType:@(EnumPickType_textfield),
                                      m1_RowPickerKeyHasArrow:@(0),
                                      m1_RowPickerKeyHoldText:@"请输入毕业院校完整名称",
                                      m1_RowPickerKeyKeyHasLine:@(1)
                                      } mutableCopy];
        _m1_pickSchool.m1_edgeInsetsRow = UIEdgeInsetsMake(0, 20, 0, 20);
        [_m1_pickSchool m1_updateData];
        WEAKSELF
        [_m1_pickSchool setM1_resultBlock:^(M1RowPickView *row) {
            [weakSelf m1_enableRightBtn];
            [weakSelf.m1_paramDic setObject:[row.m1_paramDic[m1_RowPickerKeyValue] m1_removeWhitespace] forKey:@"school"];
        }];
    }
    return _m1_pickSchool;
}

- (NSMutableDictionary *)m1_paramDic
{
    if (!_m1_paramDic) {
        _m1_paramDic = [[NSMutableDictionary alloc] init];
        M1Education *edu = [M1User user].basic.education_info;
        [_m1_paramDic setObject:m1_kSetNULLString(edu.education_code) forKey:@"education_code"];
        [_m1_paramDic setObject:m1_kSetNULLString(edu.school) forKey:@"school"];
        
        for (M1Picture *pic in edu.education) {
            [self.m1_photos addObject:pic.img.url];
        }
    }
    return _m1_paramDic;
}

#pragma mark - private
- (NSString *)m1_EduName:(NSString *)code
{
    if ([code isEqualToString:@"1"]) {
        return @"专科";
    }
    if ([code isEqualToString:@"2"]) {
        return @"本科";
    }
    if ([code isEqualToString:@"3"]) {
        return @"硕士";
    }
    if ([code isEqualToString:@"4"]) {
        return @"博士";
    }
    if ([code isEqualToString:@"5"]) {
        return @"博士后";
    }
    return @"";
}

- (NSString *)m1_EduCode:(NSString *)name
{
    if ([name isEqualToString:@"专科"]) {
        return @"1";
    }
    if ([name isEqualToString:@"本科"]) {
        return @"2";
    }
    if ([name isEqualToString:@"硕士"]) {
        return @"3";
    }
    if ([name isEqualToString:@"博士"]) {
        return @"4";
    }
    if ([name isEqualToString:@"博士后"]) {
        return @"5";
    }
    return @"";
}

#pragma mark - overwrite
- (void)m1_enableRightBtn
{
    BOOL isEnable = NO;
    if (!m1_NULLString([self.m1_pickEdu.m1_paramDic[m1_RowPickerKeyValue] m1_removeWhitespace]) && !m1_NULLString([self.m1_pickSchool.m1_paramDic[m1_RowPickerKeyValue]  m1_removeWhitespace]) && self.m1_photos.count > 1) {
        isEnable = YES;
    }
    self.navigationItem.rightBarButtonItem.enabled = isEnable;
}

#pragma mark - request
- (void)m1_requestCertify:(NSString *)imgJson
{
    [self.m1_paramDic setObject:imgJson forKey:@"pic"];
    WEAKSELF;
    NSDictionary *dic = [self.m1_paramDic copy];
    [M1HttpNet requestWithMethod:m1_education_pic_url params:dic success:^(id resDic, NSInteger code, NSString *message) {
         NSArray *array = weakSelf.navigationController.viewControllers;
        if ([array[0] isKindOfClass:[M1PersonalViewController class]]) {
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    } fail:^(id resDic, NSInteger code, NSString *message) {
        [M1Alert m1_alertWithTitle:nil message:message];
    } showAni:YES];
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.m1_photos.count == self.m1_maxPhoto + 1 ? self.m1_photos.count : self.m1_photos.count + 1;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        

        if (!_m1_pickEdu) {
            [reusableView addSubview:self.m1_pickEdu];
            self.m1_pickEdu.frame = CGRectMake(0, 10, SCREENWIDTH, 54);
        }
        if (!_m1_pickSchool) {
            [reusableView addSubview:self.m1_pickSchool];
            self.m1_pickSchool.frame = CGRectMake(0, CGRectGetMaxY(self.m1_pickEdu.frame), SCREENWIDTH, 54);
        }
        [self.m1_pickEdu m1_updateValue:[self m1_EduName:self.m1_paramDic[@"education_code"]] value:[self m1_EduName:self.m1_paramDic[@"education_code"]]];
        [self.m1_pickSchool m1_updateValue:self.m1_paramDic[@"school"] value:self.m1_paramDic[@"school"]];
        [self m1_enableRightBtn];
        
        return reusableView;
    }
    else
    {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
        return reusableView;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 150);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 50);
}

@end
